package com.efast.dmom.mgmt.service.les.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.dmom.mgmt.dao.les.TwQualityTaskDetailMapper;
import com.efast.dmom.mgmt.entity.les.TwQualityTaskDetail;
import com.efast.dmom.mgmt.service.les.TwQualityTaskDetailService;
import com.efast.dmom.mgmt.service.mes.TwBasLotService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * <p>
 * 质检任务子表 服务实现类
 * </p>
 *
 * @author ChenLiPeng
 * @since 2018-05-27
 */
@Service
public class TwQualityTaskDetailServiceImpl extends ServiceImpl<TwQualityTaskDetailMapper, TwQualityTaskDetail>
		implements TwQualityTaskDetailService {

	private final TwQualityTaskDetailMapper twQualityTaskDetailMapper;

	@Autowired
	public TwQualityTaskDetailServiceImpl(TwQualityTaskDetailMapper twQualityTaskDetailMapper) {
		this.twQualityTaskDetailMapper = twQualityTaskDetailMapper;
	}
	
	@Autowired
	private TwBasLotService twBasLotService;

	/**
	 * 扫描箱条码 或者ASN条码，获取对应的未处理的质检任务；当扫描ASN条码未扫描箱号时， 系统会按照零件包装流水号排序自动选择一条未处理的质检任务； 所以综上，质检只支持按箱进行，对于1箱零件来说，只有整体的质检通过或者不通过
	 *
	 * @param twQualityTaskDetail
	 *            twQualityTaskDetail
	 */
	@Override
	public TwQualityTaskDetail getTask(TwQualityTaskDetail twQualityTaskDetail) {

		EntityWrapper<TwQualityTaskDetail> tWrapper = new EntityWrapper<>();
		if (StringUtils.isNotBlank(twQualityTaskDetail.getAsnSheetNo())
				&& StringUtils.isNotBlank(twQualityTaskDetail.getPartPackSerial())) {
			tWrapper.where("asn_sheet_no={0}", twQualityTaskDetail.getAsnSheetNo()).and("part_pack_serial={0}",
					twQualityTaskDetail.getPartPackSerial()).and("quality_status=0");
		}
		if (StringUtils.isNotBlank(twQualityTaskDetail.getAsnSheetNo())
				&& StringUtils.isBlank(twQualityTaskDetail.getPartPackSerial())) {
			tWrapper.where("asn_sheet_no={0}", twQualityTaskDetail.getAsnSheetNo()).and("quality_status=0").orderBy(
					"part_pack_serial", true);
		}
		if (StringUtils.isBlank(twQualityTaskDetail.getAsnSheetNo())
				&& StringUtils.isNotBlank(twQualityTaskDetail.getPartPackSerial())) {
			tWrapper.where("part_pack_serial={0}", twQualityTaskDetail.getPartPackSerial()).and("quality_status=0");
		}
		if (StringUtils.isBlank(twQualityTaskDetail.getAsnSheetNo())
				&& StringUtils.isBlank(twQualityTaskDetail.getPartPackSerial())) {
			throw new ServiceException("ASN序列号和扫描条码不能都为空");
		}
		List<TwQualityTaskDetail> twQualityTaskHeaders = twQualityTaskDetailMapper.selectList(tWrapper);
		if (twQualityTaskHeaders.size() == 0) {
			throw new ServiceException("没有查询到记录");
		}
		return twQualityTaskHeaders.get(0);
	}

	/**
	 * 无论质检通过或者不通过，质检任务完成后均需要按照入库规则进行自动分配到对应的库位（和收货上架分配一样， 但是已经质检了的 就不能再次分配到质检库位了，也就是说质检完成后，入库分配时不能再次匹配到质检的规则，
	 * 需要匹配到下一条合适的规则）
	 *
	 * @param twQualityTaskDetail
	 *            twQualityTaskDetail
	 */
	@Override
	public void ifPass(TwQualityTaskDetail twQualityTaskDetail) {

		TwQualityTaskDetail twQualityTaskDetail1 = twQualityTaskDetailMapper.selectById(
				twQualityTaskDetail.getTwQualityTaskDetailId());
		// 实际质检通过数量 质检不通过为0
		// 质检任务状态  :  0-新单 1-质检通过 2-质检不通过 3-关闭
		if ("1".equals(twQualityTaskDetail.getQualityStatus())) {
			twQualityTaskDetail.setActNboundQty(twQualityTaskDetail1.getPreNboundQty());
			twQualityTaskDetail.setActQualityPackNum(twQualityTaskDetail1.getPreQualityPackNum());
			// 打印质检标签
			// twBasLotService.printQualityTestingCard(twQualityTaskDetail);
		}
		if ("2".equals(twQualityTaskDetail.getQualityStatus())) {
			twQualityTaskDetail.setActNboundQty(0);
		}
		twQualityTaskDetailMapper.updateById(twQualityTaskDetail);

		// 具体的分配的逻辑由冯总统一处理的
		// .........................

	}
}
